﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using Microhard.Core;
using FluentValidation.Results;
using Microhard.ApproveFlow.Buz.Models;
using Microhard.ApproveFlow.Buz.Interfaces;
using Microhard.ApproveFlow.Buz.Msg;

namespace Microhard.ApproveFlow.Buz.Validators
{
    public class ApproveFlowTypeValidator : AbstractValidator<ApproveFlowTypeModel>
    {
        private IApproveFlowType iaft = IoC.Resolve<IApproveFlowType>();
        public ApproveFlowTypeValidator()
        {
        
            RuleSet("Insert", () =>
            {
                RuleFor(u => u.TypeName).NotEmpty().WithMessage(ApproveFlowTypeMsg.NeedType).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.TypeRemark).Length(0, 200).WithMessage(ApproveFlowTypeMsg.MaxMark).When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);

                Custom(u =>
                {                 
                    if (iaft.IsTypeNameDuplicate(u))
                    {
                        return new ValidationFailure("TypeName", ApproveFlowTypeMsg.DuplicateTypeName);
                    }
                    return null;
                });

            });

            RuleSet("Update", () =>
            {
                RuleFor(u => u.TypeName).NotEmpty().WithMessage(ApproveFlowTypeMsg.NeedType);
                RuleFor(u => u.TypeRemark).Length(0, 200).WithMessage(ApproveFlowTypeMsg.MaxMark);

                Custom(u =>
                {
                    if (iaft.IsTypeNameDuplicate(u))
                    {
                        return new ValidationFailure("TypeName", ApproveFlowTypeMsg.DuplicateTypeName);
                    }
                    return null;
                });
            });
        }
    }
}
